package com.yfp.operation.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.poi.ExcelUtil;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.operation.domain.UserBill;
import com.yfp.operation.domain.dto.SubAccountDTO;
import com.yfp.operation.domain.vo.SharingVO;
import com.yfp.operation.domain.vo.SubAccountVO;
import com.yfp.operation.service.AccountService;
import com.yfp.operation.service.UserBillService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账户
 *
 *
 * @version 1.0
 * @date 2024-06-11 15:52
 */
@RestController
@RequestMapping("/accountfde")
@Tag(name = "投资人-pc")
public class AccountController extends BaseController {

    @Resource
    private AccountService accountService;
    @Resource
    private UserBillService userBillService;


    /**
     * 获取设备分账列表
     *
     * @param subAccountDTO
     * @return
     */
    @GetMapping("/postnfd")
    @Operation(summary = "获取设备分账详情列表", description = "详情页面使用")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = SubAccountVO.class)
                    ))
            }
    )
    public R<TableDataInfo> subAccount(SubAccountDTO subAccountDTO) {
        startPage();
        List<String> machineIdList = userBillService.getMachineId(subAccountDTO);
        TableDataInfo dataTable = getDataTable(machineIdList);
        dataTable.setRows(accountService.subAccount(machineIdList,subAccountDTO));
        return R.ok(dataTable);
    }


    /**
     * 获取订单分账列表
     *
     * @param subAccountDTO
     * @return
     */
    @GetMapping("/userbill")
    @Operation(summary = "获取订单分账详情列表", description = "详情页面使用")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = UserBill.class)
                    ))
            }
    )
    public R<TableDataInfo> selectAllUserBill(SubAccountDTO subAccountDTO) {
        startPage();
        List<UserBill> userBills = accountService.selectAllUserBill(subAccountDTO);
        TableDataInfo dataTable = getDataTable(userBills);
        List<UserBill> billList = accountService.supplement(dataTable.getRows(), subAccountDTO.getAccountNumber());
        dataTable.setRows(billList);

        return R.ok(dataTable);
    }


    /**
     * 创客分账统计列表
     *
     * @param accountNumber
     * @return
     */
    @GetMapping("/sharingbill")
    @Operation(summary = "创客分账统计列表", description = "详情页面使用")
    @ApiResponses(value =
            {
                    @ApiResponse(responseCode = "200", description = "Successful response", content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = SharingVO.class)
                    ))
            }
    )
    public R<TableDataInfo> selectSharing(String accountNumber) {
        startPage();
        List<Integer> userIds = userBillService.getUserId(accountNumber);
        TableDataInfo dataTable = getDataTable(userIds);
        List<SharingVO> list = accountService.selectSharing(userIds);
        dataTable.setRows(list);
        return R.ok(dataTable);
    }


    @PostMapping("/frgter")
    @Operation(summary = "设备分账列表数据导出", description = "数据导出")
    @Log(title = "分账-设备分账列表数据导出", businessType = BusinessType.EXPORT)
    public void machineExport(HttpServletResponse response, @ModelAttribute SubAccountDTO subAccountDTO) {
        List<String> machineIdList = userBillService.getMachineId(subAccountDTO);
        List<SubAccountVO> list = accountService.subAccount(machineIdList,subAccountDTO);
        ExcelUtil<SubAccountVO> util = new ExcelUtil<SubAccountVO>(SubAccountVO.class);
        util.exportExcel(response, list, "设备分账数据");
    }

    @PostMapping("/bghighuie")
    @Operation(summary = "订单分账列表数据导出", description = "数据导出")
    @Log(title = "分账-订单分账列表数据导出", businessType = BusinessType.EXPORT)
    public void comboExport(HttpServletResponse response, @ModelAttribute SubAccountDTO subAccountDTO) {
        List<UserBill> list = accountService.selectAllUserBill(subAccountDTO);
        ExcelUtil<UserBill> util = new ExcelUtil<UserBill>(UserBill.class);
        util.exportExcel(response, list, "订单分账数据");
    }

}
